package com.intelligent.ispc.foundation.pdf;

import com.intelligent.ispc.common.utils.ApplicationException;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.List;

/**
 * Created by Eligi.ran on 2016/9/1.
 */
public class PdfComponentFactory {
    public final static int HORIZONTAL = 0;
    public final static int VERTICAL = 1;

    public static PdfPTable paintTable(List<String> metadatas, Object[][] values, TableStyleConfig styleConfig) {
        if (metadatas == null || values == null || styleConfig == null) {
            throw new NullPointerException();
        }

        int rowNum = values.length;
        int colNum = values[0].length;
        int metadatasNum = metadatas.size();
        if (colNum != metadatasNum) {
            throw new InvalidParameterException("metadatas size is not equal to values length");
        }

        int arrangeStyle = styleConfig.getCurrentLayout();
        int tableColNum;
        PdfPTable pdfPTable = null;
        PdfPCell pdfPCell = new PdfPCell();
        Font font = new Font();
        font.setSize(10);


        if (arrangeStyle == HORIZONTAL) {
            tableColNum = metadatasNum;
            pdfPTable = new PdfPTable(tableColNum);
            pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //set filed names
            for (String metadata : metadatas) {
                PdfPCell nameCell = new PdfPCell(pdfPCell);
                nameCell.setPhrase(new Paragraph(metadata, font));
                pdfPTable.addCell(nameCell);
            }
            //set value for each field
            for (int i = 0; i < rowNum; i++) {
                for (int j = 0; j < colNum; j++) {
                    PdfPCell dataCell = new PdfPCell(pdfPCell);
                    dataCell.setPhrase(new Paragraph(values[i][j].toString(), font));
                    pdfPTable.addCell(dataCell);

                }
            }
        } else if (arrangeStyle == VERTICAL) {
            tableColNum = rowNum + 1;
            pdfPTable = new PdfPTable(tableColNum);

            pdfPCell.setBorder(Rectangle.NO_BORDER);
            pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);
            font.setSize(7);
            for (int i = 0; i < colNum; i++) {
                String metadata = metadatas.get(i);
                PdfPCell nameCell = new PdfPCell(pdfPCell);
                nameCell.setPhrase(new Paragraph(metadata+":", font));
                pdfPTable.addCell(nameCell);
                for (int j = 0; j < rowNum; j++) {
                    PdfPCell valueCell = new PdfPCell(pdfPCell);
                    nameCell.setPhrase(new Paragraph(values[j][i].toString(), font));
                    pdfPTable.addCell(nameCell);
                }
            }

        }
        return pdfPTable;

    }

    public static Image paintImage(String imagePath, ImageStyleConfig styleConfig) {
        if (imagePath == null)
            return null;
        Image image = null;
        try {
            image = Image.getInstance(imagePath);
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

}
